
package it.bd.SetAllConnectionsOff.helper;

import android.content.ContextWrapper;
import android.util.Log;

import it.bd.SetAllConnectionsOff.SetAllConnectionsOff;

public class StateHelper extends APreferenceAware<Boolean> {

    public static final String PREFS_KEY = "limitedState";
    private static StateHelper instance  = null;

    public static StateHelper getInstance() {
        if (instance == null) {
            Log.w("", "StateHelper not initialized !");
        }
        return instance;

    }

    private ContextWrapper context;

    public StateHelper(ContextWrapper context) {
        this.context = context;
        instance = this;
    }

    @Override
    ContextWrapper getContext() {
        return context;
    }

    @Override
    String getPrefKey() {
        return PREFS_KEY;
    }

    public boolean switchState() {
        boolean on = getState();
        on = !on;
        setState(on);
        return on;
    }

    public boolean getState() {
        Boolean state = getSavedStateValue();
        if (state == null) {
            state = false;
        }
        return state;
    }

    public void setState(boolean limited) {
        boolean on = getState();
        if (limited && !on) {
            startLimitedState();
            setSavedStateValue(true);
        } else if (!limited && on) {
            endLimitedState();
            setSavedStateValue(false);
        }
    }

    private void endLimitedState() {
        if (SetAllConnectionsOff.helpers != null) {
            for (AServiceHelper helper : SetAllConnectionsOff.helpers) {
                if (helper.isServicePresent()) {
                    if (helper.getLimitedStateValue() != null && helper.getLimitedStateValue() && helper.getSavedStateValue() != null
                        && helper.getSavedStateValue()) {
                        helper.setServiceEnabled(helper.getSavedStateValue());
                    }
                }
            }
        }

        // TODO:add a check for the brightness
        PowerHelper ph = PowerHelper.getInstance();
        Integer b = ph.getLimitedStateValue();
        if (b != null && b > 0) {
            Integer b1 = ph.getSavedStateValue();
            if (b1 != null) {
                if (b1 == -1) {
                    ph.setBrightnessAutomatic(true);
                } else {
                    ph.setBrightnessAutomatic(false);
                    ph.setScreenBrighteness(ph.getSavedStateValue());
                }
            }
        }
    }

    private void startLimitedState() {
        if (SetAllConnectionsOff.helpers != null) {
            for (AServiceHelper helper : SetAllConnectionsOff.helpers) {
                if (helper.isServicePresent()) {
                    if (helper.getLimitedStateValue() != null && helper.getLimitedStateValue()) {
                        helper.setSavedStateValue(helper.isServiceEnabled());
                        helper.setServiceEnabled(false);
                    } else {
                        helper.deleteLimitedStateValue();
                        helper.deleteSavedStateValue();
                    }
                }
            }
        }
        // TODO:add a check for the brightness
        PowerHelper ph = PowerHelper.getInstance();
        Integer b = ph.getLimitedStateValue();
        if (b != null && b > 0) {
            int currentVal = PowerHelper.getInstance().isBrightnessAutomatic() ? -1 : PowerHelper.getInstance().getScreenBrighteness();
            if (currentVal < 0) {
                ph.setBrightnessAutomatic(false);
            }
            ph.setSavedStateValue(currentVal);
            ph.setScreenBrighteness(b);
        }
    }

}
